package cn.com.nxt.txb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import cn.com.nxt.txb.httpdelegate.LinkDelegate;
import cn.com.nxt.txb.httpdelegate.UserDelegate;
import cn.com.nxt.txb.model.BaseModel;
import cn.com.nxt.txb.model.User;
import cn.com.nxt.txb.util.AppConstant;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.CountDownTimerUtils;
import cn.com.nxt.txb.widget.ClearEditText;

@ContentView(R.layout.activity_register)
public class RegisterActivity extends BaseActivity {
    @ViewInject(R.id.et_username)
    private ClearEditText mUsernameEt;
    @ViewInject(R.id.et_phone)
    private ClearEditText mPhoneEt;
    @ViewInject(R.id.et_code)
    private ClearEditText mCodeEt;
    @ViewInject(R.id.et_password)
    private ClearEditText mPwdEt;
    @ViewInject(R.id.et_password2)
    private ClearEditText mPwdEt2;
    private String mUsername, mPhone, mCode, mPwd, mPwd2;
    private UserDelegate delegate;
    private byte registerFlag = 0x031;
    private byte smsVertifyFlag = 0x032;
    private byte smsVertifyCheckFlag = 0x033;

    @ViewInject(R.id.iv_eye)
    private ImageView mIvEye;
    @ViewInject(R.id.iv_eye2)
    private ImageView mIvEye2;
    private boolean mIsPasswordShow = false;
    private boolean mIsPasswordShow2 = false;
    private CountDownTimerUtils countDownTimer;// 发送成功进入倒计时
    @ViewInject(R.id.rst_send_code)
    private TextView mTvSendCode;
    private LinkDelegate linkDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        delegate = new UserDelegate(RegisterActivity.this);
        linkDelegate = new LinkDelegate(this);
        countDownTimer = new CountDownTimerUtils(mTvSendCode, 60000, 1000);
    }

    @Event({R.id.tv_register, R.id.iv_eye, R.id.iv_eye2, R.id.rst_send_code})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.tv_register:
                register();
                break;
            case R.id.iv_eye:
                passwordShowOrHidden();
                break;
            case R.id.iv_eye2:
                passwordShowOrHidden2();
                break;
            case R.id.rst_send_code:
                sendCode();
                break;
        }
    }

    private void sendCode() {
        mPhone = mPhoneEt.getText().toString().trim();
        if (TextUtils.isEmpty(mPhone)) {
            showToast("手机号不能为空！");
            return;
        }
        if (!AppUtil.isPhoneNumberValid(mPhone)) {
            showToast("手机号格式错误！");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("phone", mPhone);
        Gson gson = new Gson();
        linkDelegate.smsVertify(gson.toJson(map), smsVertifyFlag);
        countDownTimer.start();
    }

    private void passwordShowOrHidden() {
        if (mIsPasswordShow) {
            //选择状态 显示明文--设置为可见的密码
            mPwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            mIvEye.setImageResource(R.drawable.eye_close);
        } else {
            //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
            mPwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            mIvEye.setImageResource(R.drawable.eye);
        }
        mIsPasswordShow = !mIsPasswordShow;
    }

    private void passwordShowOrHidden2() {
        if (mIsPasswordShow2) {
            //选择状态 显示明文--设置为可见的密码
            mPwdEt2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            mIvEye2.setImageResource(R.drawable.eye_close);
        } else {
            //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
            mPwdEt2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            mIvEye2.setImageResource(R.drawable.eye);
        }
        mIsPasswordShow2 = !mIsPasswordShow2;
    }


    private void register() {
        mUsername = mUsernameEt.getText().toString().trim();
        if (TextUtils.isEmpty(mUsername)) {
            showToast("用户名不能为空！");
            return;
        }
        mPhone = mPhoneEt.getText().toString().trim();
        if (TextUtils.isEmpty(mPhone)) {
            showToast("手机号不能为空！");
            return;
        }
        if (!AppUtil.isPhoneNumberValid(mPhone)) {
            showToast("手机号格式错误！");
            return;
        }
        mCode = mCodeEt.getText().toString().trim();
        if (TextUtils.isEmpty(mCode)) {
            showToast("验证码不能为空！");
            return;
        }
        mPwd = mPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mPwd)) {
            showToast("密码不能为空！");
            return;
        }
        mPwd2 = mPwdEt2.getText().toString().trim();
        if (TextUtils.isEmpty(mPwd)) {
            showToast("确认密码不能为空！");
            return;
        }
        if (!mPwd.equals(mPwd2)) {
            showToast("再次密码输入不一致！");
            return;
        }
        linkDelegate.smsVertifyCheck(mCode,mPhone,smsVertifyCheckFlag);
        showDialog("正在检查验证码");
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        disDialog();
        if (flag == registerFlag) {
            Gson gson = new Gson();
            User user = gson.fromJson(result, User.class);
            user.setPhone(mPhone);
            //AppUtil.mUser=user;
            // AppUtil.saveUser(RegisterActivity.this,user);
            showToast("注册成功");
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("user", user);
            intent.putExtras(bundle);
            setResult(LoginActivity.RESULT_REGISTER, intent);
            finish();
        }else if(flag==smsVertifyCheckFlag){
            showDialog("正在注册");
            User user = new User();
            user.setUsername(mUsername);
            user.setPhone(mPhone);
            user.setPassword(mPwd);
            Gson gson = new Gson();
            Log.v("--register--", gson.toJson(user));
            delegate.register(gson.toJson(user), registerFlag);
        }
    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {
        super.failure(result, flag, status);
        disDialog();
        if (flag == registerFlag) {
            showToast(result);
        }else if(flag==smsVertifyFlag){
            showToast(result);
        }else if(flag==smsVertifyCheckFlag){
            showToast(result);
        }
    }

    @Override
    public void finished(int flag) {
        disDialog();
    }

    @Override
    public void cancelled(int flag) {
        disDialog();
    }
}
